const dataAccess = require('../dataAccess');
var modelIgnore = require('../config').modelIgnore;
const sysModel = dataAccess["main"].model("sysModel");
var getModelAttrs = require('./getModelAttrs')
var mkdirByPath = require("../utils/mkdirByPath")
var writeJsFile = require("../utils/writeJsFile")
async function main() {
    try {
        var modelPath = mkdirByPath()
        var exportStr = "export default {"
        var importStr = ""
        
        var modelList = await sysModel.findAll({});
        modelList.forEach(function(element,index){
            var model = element.get({ plain: true });
            var ignoreStatus = false
            for (var i = 0; i < modelIgnore.length; i++) {
                if(model.className==modelIgnore[i]){
                    ignoreStatus=true
                }
            }
            if(ignoreStatus==false){
                
                var modelFileName = model.modelAlName.charAt(0).toLowerCase() + model.modelAlName.slice(1);
                
                importStr = importStr+"import "+modelFileName+" from './"+modelFileName+"'"+'\n'
            
                exportStr=exportStr+"\n\t"+modelFileName+","
                getModelAttrs(element.get({ plain: true }))
            }
        });
    

        var indexStr = importStr+"\n"+exportStr+"\n"+"}"
        // writeJsFile(modelPath+"/index.js",indexStr)
        // console.log("index.js已生成")
        console.log("文件所处位置"+modelPath)


    } catch (e) {

        console.log(e);
    } finally {

        console.log("操作完成！！！");
    }
}
main()